using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Acms.Bll;

public partial class Users_Details : BaseUserPage
{
    public override Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.MembershipAdministrator, Role.Roles.MembershipViewer }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                id = int.Parse(Request.QueryString["Id"]);
            }
            catch (FormatException)
            {
            }
            catch (ArgumentNullException)
            {
            }
            
            Acms.Bll.Role.Roles[] editRoles = new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.MembershipAdministrator };
            lnkEdit.Visible = User.HasRole(editRoles);
            lnkSave.Visible = false;
            lnkCancel.Visible = false;

            foreach (DataControlField field in dvUser.Fields)
            {
                if (field is Acms.WebControls.DateField)
                {
                    Acms.WebControls.DateField tmp = (Acms.WebControls.DateField)field;
                    tmp.MinValue = DateTime.Now.AddYears(-120);
                    tmp.MaxValue = DateTime.Now;
                }
            }

            if (base.CurrentUser != null)
            {
                CustomFields = new string[CurrentUser.CustomProperties.Count];
                for (int i = 0; i < CurrentUser.CustomProperties.Count; i++)
                {
                    CustomFields[i] = CurrentUser.CustomProperties[i].Name;
                }
            }
            CreateCustomFields();
        }

    }

    private void CreateCustomFields()
    {
        for (int i = 0; i < CustomFields.Length; i++)
        {
            Acms.WebControls.TextBoxField tmp = new Acms.WebControls.TextBoxField();
            tmp.HeaderText = CustomFields[i];
            tmp.DataField = string.Format("CustomProperties[{0}].Value", i);
            dvUser.Fields.Add(tmp);
        }
    }

    private int id;

    private string[] CustomFields;

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["Id"] is int)
        {
            id = (int)ViewState["Id"];
        }
        CustomFields = ViewState["CustomFields"] as string[];
        CreateCustomFields();

    }

    protected override object SaveViewState()
    {
        ViewState["Id"] = id;
        ViewState["CustomFields"] = CustomFields;
        return base.SaveViewState();
    }

    protected void lnkEdit_Click(object sender, EventArgs e)
    {
        dvUser.ChangeMode(DetailsViewMode.Edit);
        lnkEdit.Visible = false;
        lnkSave.Visible = true;
        lnkCancel.Visible = true;
    }

    protected void dsUser_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.User> e)
    {
        Acms.Bll.User user = Acms.Bll.User.GetById(id);
        if (user != null && (user.SiteId == AcmsContext.SiteId || user.SiteId == 0))
        {
            e.SetData(user);
        }
    }

    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        lnkSave.Visible = false;
        lnkEdit.Visible = true;
        lnkCancel.Visible = false;

        dvUser.ChangeMode(DetailsViewMode.ReadOnly);
    }

    protected void lnkSave_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            dvUser.UpdateItem(true);
        }
    }

    protected void dvUser_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.Exception == null)
        {
            lnkEdit.Visible = true;
            lnkSave.Visible = false;
            lnkCancel.Visible = false;
        }
    }

    protected void dsUser_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.User> e)
    {
        for (int i = 0; i < e.Item.CustomProperties.Count; i++)
        {
            string field = string.Format("CustomProperties[{0}].Value", i);
            if (e.ValueChanged<string>(field))
            {
                e.Item.CustomProperties[i].Value = e.GetNewValue<string>(field);
                e.Item.CustomProperties[i].Update();
            }
        }
    }
}
